/*
//      k2rtapp.c
//      
// 		Copyright (C) 2010- Valentina Vuksic <v@valvuk.ch>
//      Copyright (C) 2010- Beat Michel Liechti <bml@maduraheights.ch>
//      
//      This program is free software; you can redistribute it and/or modify
//      it under the terms of the GNU General Public License as published by
//      the Free Software Foundation; either version 2 of the License, or
//      (at your option) any later version.
//      
//      This program is distributed in the hope that it will be useful,
//      but WITHOUT ANY WARRANTY; without even the implied warranty of
//      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//      GNU General Public License for more details.
//      
//      You should have received a copy of the GNU General Public License
//      along with this program; if not, write to the Free Software
//      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
//      MA 02110-1301, USA.
*/

#include <stdlib.h>
#include <stdio.h>
#include <sys/mman.h>	/* for MCL_CURRENT and MCL_FUTURE */
#include <rtdm/rtdm.h>
#include <native/task.h>

#define DEVICE_NAME			"k2rtdev"

int main(int argc, char *argv[])
{
	char buf[1024];
	ssize_t size;
	int i, retval, wheelspeed;
	
	/* -- ask user for text to be displayed */
	printf("please enter text to write on wheel: ");
	scanf("%s", buf);
	/* -- check that text has at least one character */
	if (strlen(buf) < 1) {
		printf("\nsorry, invalid text, aborting\n");
		retval = -1;
	}
	else {
		/* -- convert characters to upper case */
		for (i = 0; buf[i]; i++) {
			buf[i] = toupper(buf[i]);
		}
		printf("\nwriting text %s\n", buf);
		/* -- ask user for required speed */
		printf("\nplease enter desired wheel speed (2-100): ");
		scanf("%d", &wheelspeed);
	
		if (wheelspeed < 0 || wheelspeed > 100) {
			printf("\nsorry, invalid wheel speed, aborting\n");
			retval = -2; 
		}
		else
		{
			printf("\opening device...\n");
			/* -- open the device */
			int fd_device = rt_dev_open(DEVICE_NAME, 0);
			if (fd_device < 0) {
				printf("ERROR : can't open device %s (%s)\n",
					   DEVICE_NAME, strerror(-fd_device));
				fflush(stdout);
				retval = 1;
			}
			else {
				printf("\nsetting wheel speed...\n");
				/* -- set wheel speed through ioctl */
				retval = rt_dev_ioctl(fd_device, wheelspeed);
				if (retval != 0) {
					printf("ERROR : can't set wheel speed through rt_dev_ioctl on device %s (%s)\n",
						   DEVICE_NAME, strerror(-retval));
					fflush(stdout);
					retval = 2;
				}
				else
				{
					printf("\nwriting text...\n");
					/* -- write desired text */
					size = rt_dev_write(fd_device, (const void *)buf, strlen(buf));
					
					printf("\nstopping wheel...\n");
					/* -- stop wheel by setting speed to zero */
					retval = rt_dev_ioctl(fd_device, 0);
					if (retval != 0) {
						printf("ERROR : can't set wheel speed through rt_dev_ioctl on device %s (%s)\n",
							   DEVICE_NAME, strerror(-retval));
						fflush(stdout);
						retval = 3;
					}
					else {
						retval = 0;
					}
				}
				
				printf("\nclosing device...\n");
				/* close the device */
				retval = rt_dev_close(fd_device);
				if (retval != 0) {
					printf("ERROR : can't close device %s (%s)\n",
						   DEVICE_NAME, strerror(-retval));
					fflush(stdout);
					retval = 4;
				}
			}
		}
	}
	
	/*
	struct timespec a, b;
	a.tv_sec = 5;
	a.tv_nsec = 0;
	clock_nanosleep(CLOCK_REALTIME, 0, &a, &b);
	*/
	return retval;
}
